import { Injectable } from '@angular/core';
import {
    Router, Resolve,
    ActivatedRouteSnapshot
} from '@angular/router';

import { PlayerService } from '../http/http-player.service';
import { Player } from '../http/player';

@Injectable()
export class RouterPlayerDetailResolveService implements Resolve<Player> {

    constructor(private playerService: PlayerService, private router: Router) { }

    resolve(route: ActivatedRouteSnapshot): Promise<Player> | boolean {
        let id = route.params['id'];

        return this.playerService.getPlayer(id).toPromise().then(player => {
            if (player) {
                return player;
            } else { // id not found
                this.router.navigate(['/router']);
                return false;
            }
        });
    }
}